<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" label-width="68px">
     <!-- <el-form-item label="用户标识" prop="goodsEvaluationUserId">
        <el-input
          v-model="queryParams.goodsEvaluationUserId"
          placeholder="请输入用户标识—外键"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="商品标识" prop="goodsEvaluationGoodsId">
        <el-input
          v-model="queryParams.goodsEvaluationGoodsId"
          placeholder="请输入商品标识"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="评论内容" prop="goodsEvaluationContent">
        <el-input
          v-model="queryParams.goodsEvaluationContent"
          placeholder="请输入评论内容"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item> -->
      <el-form-item label="星级评分" prop="goodsEvaluationStarLevel">
        <el-input
          v-model="queryParams.goodsEvaluationStarLevel"
          placeholder="请输入整体星级评分"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="物流评分" prop="goodsEvaluationLogistics">
        <el-input
          v-model="queryParams.goodsEvaluationLogistics"
          placeholder="请输入物流评分"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
    <!--  <el-form-item label="描述相符评分" prop="goodsEvaluationServiceAttitude">
        <el-input
          v-model="queryParams.goodsEvaluationServiceAttitude"
          placeholder="请输入描述相符评分"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="订单标识—外键" prop="goodsEvaluationOrdersId">
        <el-input
          v-model="queryParams.goodsEvaluationOrdersId"
          placeholder="请输入订单标识—外键"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item> -->
      <el-form-item label="添加时间" prop="goodsEvaluationTime">
        <el-date-picker clearable size="small" style="width: 200px"
          v-model="queryParams.goodsEvaluationTime"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="选择添加时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <!-- <el-col :span="1.5">
        <el-button
          type="primary"
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['system:evaluation:add']"
        >新增</el-button>
      </el-col> -->
      <el-col :span="1.5">
        <el-button
          type="success"
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['system:evaluation:edit']"
        >修改</el-button>
      </el-col>
      <!-- <el-col :span="1.5">
        <el-button
          type="danger"
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['system:evaluation:remove']"
        >删除</el-button>
      </el-col> -->
      <el-col :span="1.5">
        <el-button
          type="warning"
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['system:evaluation:export']"
        >导出</el-button>
      </el-col>
    </el-row>

    <el-table v-loading="loading" :data="evaluationList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="用户名称" align="center" prop="goodsEvaluationUserId">
		  <template slot-scope="scope">
		  	{{scope.row.personage.username}}
		  </template>
	  </el-table-column>
      <el-table-column label="商品名称" align="center" prop="goodsEvaluationGoodsId">
		  <template slot-scope="scope">
		  	{{scope.row.goods.goodsName}}
		  </template>
	  </el-table-column>
	  <el-table-column label="商品图片" align="center" prop="goodsThumbnailImg">
		 <template style="width: 100px; height: 100px;border-radius: 50%;"
		   slot-scope="scope" v-if="scope.row.goods.goodsThumbnailImg != null && scope.row.goods.goodsThumbnailImg != ''">
		<el-popover trigger="hover" placement="top">
			<img v-for="(item, index) in scope.row.goods.goodsThumbnailImg.split(',')" style="width: 100px;margin-left: 5px;" :key="index" :src="item" />
			<div slot="reference" class="name-wrapper"><el-tag size="medium">查看</el-tag></div>
		</el-popover>
		  </template>
	  </el-table-column>
      <el-table-column label="评论内容" align="center" prop="goodsEvaluationContent" width="150"/>
      <el-table-column label="评论图片" align="center" prop="goodsEvaluationImg">
		  <template style="width: 100px; height: 100px;border-radius: 50%;"
			slot-scope="scope" v-if="scope.row.goodsEvaluationImg != null && scope.row.goodsEvaluationImg != ''">
			<el-popover trigger="hover" placement="top">
				<img v-for="(item, index) in scope.row.goodsEvaluationImg.split(',')" style="width: 100px;margin-left: 5px;" :key="index" :src="item" />
				<div slot="reference" class="name-wrapper"><el-tag size="medium">查看</el-tag></div>
			</el-popover>
		   </template>
	  </el-table-column>
      <el-table-column label="整体星级评分(%)" align="center" prop="goodsEvaluationStarLevel" width="120">
		  <template slot-scope="scope">
		  	{{scope.row.goodsEvaluationStarLevel +'%'}}
		  </template>
	  </el-table-column>
      <el-table-column label="物流评分(%)" align="center" prop="goodsEvaluationLogistics" width="120">
		  <template slot-scope="scope">
		  	{{scope.row.goodsEvaluationLogistics +'%'}}
		  </template>
	  </el-table-column>
      <el-table-column label="描述相符评分(%)" align="center" prop="goodsEvaluationServiceAttitude" width="120">
		  <template slot-scope="scope">
		  	{{scope.row.goodsEvaluationServiceAttitude +'%'}}
		  </template>
	  </el-table-column>
      <el-table-column label="订单号" align="center" prop="goodsEvaluationOrdersId">
		  <template slot-scope="scope">
		  	{{scope.row.orders.ordersCode}}
		  </template>
	  </el-table-column>
      <el-table-column label="添加时间" align="center" prop="goodsEvaluationTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.goodsEvaluationTime, '{y}年{m}月{d} {h}时{m}分') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['system:evaluation:edit']"
          >修改</el-button>
         <!-- <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:evaluation:remove']"
          >删除</el-button> -->
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改商品评价对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="用户标识—外键" prop="goodsEvaluationUserId">
          <el-input v-model="form.goodsEvaluationUserId" placeholder="请输入用户标识—外键" />
        </el-form-item>
        <el-form-item label="商品标识" prop="goodsEvaluationGoodsId">
          <el-input v-model="form.goodsEvaluationGoodsId" placeholder="请输入商品标识" />
        </el-form-item>
        <el-form-item label="评论内容" prop="goodsEvaluationContent">
          <el-input v-model="form.goodsEvaluationContent" placeholder="请输入评论内容" />
        </el-form-item>
        <el-form-item label="评论图片" prop="goodsEvaluationImg">
          <el-upload
              action="https://jsonplaceholder.typicode.com/posts/"
            	:auto-upload="false"
            	ref="upload"
            	list-type="picture-card"
            	:http-request="uploadFile"
            	:limit="1"
            	:on-preview="handlePictureCardPreview"
            	:on-remove="handleRemove"
            	:on-success="imageChange"
            	:file-list="imageList"
            ></el-upload>
            <el-dialog :visible.sync="dialogVisible"><img width="100%" :src="dialogImageUrl" alt="" /></el-dialog>
        </el-form-item>
        <el-form-item label="整体星级评分" prop="goodsEvaluationStarLevel">
          <el-input v-model="form.goodsEvaluationStarLevel" placeholder="请输入整体星级评分" />
        </el-form-item>
        <el-form-item label="物流评分" prop="goodsEvaluationLogistics">
          <el-input v-model="form.goodsEvaluationLogistics" placeholder="请输入物流评分" />
        </el-form-item>
        <el-form-item label="描述相符评分" prop="goodsEvaluationServiceAttitude">
          <el-input v-model="form.goodsEvaluationServiceAttitude" placeholder="请输入描述相符评分" />
        </el-form-item>
        <el-form-item label="订单标识—外键" prop="goodsEvaluationOrdersId">
          <el-input v-model="form.goodsEvaluationOrdersId" placeholder="请输入订单标识—外键" />
        </el-form-item>
        <el-form-item label="添加时间" prop="goodsEvaluationTime">
          <el-date-picker clearable size="small" style="width: 200px"
            v-model="form.goodsEvaluationTime"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="选择添加时间">
          </el-date-picker>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listEvaluation, getEvaluation, delEvaluation, addEvaluation, updateEvaluation, exportEvaluation ,uploadImgs } from "@/api/system/evaluation";

export default {
  name: "Evaluation",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 总条数
      total: 0,
      // 商品评价表格数据
      evaluationList: [],
      // 弹出层标题
      title: "",
	  //开始
	  formDate: null,
	  uploadList: [],
	  dialogImageUrl: '',
	  dialogVisible: false,
	  imageList: [],
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        goodsEvaluationUserId: undefined,
        goodsEvaluationGoodsId: undefined,
        goodsEvaluationContent: undefined,
        goodsEvaluationImg: undefined,
        goodsEvaluationStarLevel: undefined,
        goodsEvaluationLogistics: undefined,
        goodsEvaluationServiceAttitude: undefined,
        goodsEvaluationOrdersId: undefined,
        goodsEvaluationTime: undefined
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询商品评价列表 */
    getList() {
      this.loading = true;
      listEvaluation(this.queryParams).then(response => {
        this.evaluationList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
		this.imageList = [];
      this.form = {
        goodsEvaluationId: undefined,
        goodsEvaluationUserId: undefined,
        goodsEvaluationGoodsId: undefined,
        goodsEvaluationContent: undefined,
        goodsEvaluationImg: undefined,
        goodsEvaluationStarLevel: undefined,
        goodsEvaluationLogistics: undefined,
        goodsEvaluationServiceAttitude: undefined,
        goodsEvaluationOrdersId: undefined,
        goodsEvaluationTime: undefined
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.goodsEvaluationId)
      this.single = selection.length!=1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加商品评价";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const goodsEvaluationId = row.goodsEvaluationId || this.ids
      getEvaluation(goodsEvaluationId).then(response => {
		  if (response.data.goodsEvaluationImg != null && response.data.goodsEvaluationImg != '') {/* 判断图片的地址是否为空 */
		  	var imageList = response.data.goodsEvaluationImg.split(',');
		  	for (var i = 0; i < imageList.length; i++) {
		  		imageList[i] = imageList[i];
		  		this.imageList.push({
		  			name: imageList[i],
		  			url: imageList[i]
		  		});/*
		      alert(imageList+"：查询显示的路径"); */
		  	}
		  }
        this.form = response.data;
        this.open = true;
        this.title = "修改商品评价";
      });
    },
    /** 提交按钮 */
    submitForm: function() {
		this.formDate = new FormData();//图片
		this.$refs.upload.submit();
		console.log(this.formDate);
		if (this.uploadList.length > 0) {
		      	this.uploadList = [];
		      	uploadImgs(this.formDate).then(responses => {
		      		this.formDate = null;
		      		if (responses.code === 200) {
		      			this.$refs.upload.clearFiles();//清除上传列表
		      			var imageList = [];
		      			if (this.imageList != null) {
		      				for (var i = 0; i < this.imageList.length; i++) {
		      					if (this.imageList[i] != null) {
		      						imageList.push(this.imageList[i].name);
		      					}
		      				}
		      			}
		      			imageList = imageList.concat(responses.data);
		            console.log(responses.data);
		      			this.form.goodsEvaluationImg = imageList.join(',');
		      			this.submitForms();
		      		} else {
		      			this.msgError(responses.msg);
		      		}
		      	});
		      }else {
					this.formDate = null;
					var imageList = [];
					if (this.imageList != null) {
						for (var i = 0; i < this.imageList.length; i++) {
							if (this.imageList[i] != null) {
								imageList.push(this.imageList[i].name);
							}
						}
					}
					console.log(imageList);
					this.form.goodsEvaluationImg = imageList.join(',');
					this.submitForms();
				}
		},
		submitForms:function(){
			this.$refs["form"].validate(valid => {
			  if (valid) {
			    if (this.form.goodsEvaluationId != undefined) {
			      updateEvaluation(this.form).then(response => {
			        if (response.code === 200) {
			          this.msgSuccess("修改成功");
			          this.open = false;
			          this.getList();
			        }
			      });
			    } else {
			      addEvaluation(this.form).then(response => {
			        if (response.code === 200) {
			          this.msgSuccess("新增成功");
			          this.open = false;
			          this.getList();
			        }
			      });
			    }
			  }
			});
		},
		
    /** 删除按钮操作 */
    handleDelete(row) {
      const goodsEvaluationIds = row.goodsEvaluationId || this.ids;
      this.$confirm('是否确认删除商品评价编号为"' + goodsEvaluationIds + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return delEvaluation(goodsEvaluationIds);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        }).catch(function() {});
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm('是否确认导出所有商品评价数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return exportEvaluation(queryParams);
        }).then(response => {
          this.download(response.msg);
        }).catch(function() {});
    },
	/** 图片操作 */
	   handleRemove(file, fileList) {//删除时
	   	for (var i = 0; i < this.imageList.length; i++) {
	   		if (this.imageList[i].url == file.url) {
	   			this.imageList.splice(i, 1);
	   		}
	   	}
	   },
	   handlePictureCardPreview(file) {//上传文件时
	     console.log(file.url);
	   	this.dialogImageUrl = file.url;
	   	this.dialogVisible = true;
	   },
	   imageChange(event, file, fileList) {//成功后
	    /* alert("123成功"); */
	   	console.log(event);
	   	console.log(file);
	   	console.log(fileList);
	   },
	   uploadFile(file) {//上传行为
	   	this.uploadList.push(file.file);
	   	this.formDate.append('file', file.file);
	     console.log(this.formDate.file);
	   }
  }
};
</script>